06. Better with Mines
Prepare
It’s time to add mines to the grid. This exercise is going to rely more heavily on you, but if you lean on your knowledge of defining and initializing structs, then you’ll be just fine.
When we first create the Mine struct, an error will appear that can be ignored for now.
You should start by creating a Mine struct like the image above. You'll notice there is some weird syntax using Mine and probably a compiler error. You can ignore Mine and any errors for now, it uses a feature of Swift called protocols that we will discuss in a later session.
Learn
Your job is to complete the definition of the Mine struct. It needs two more properties:
- a constant
GridLocationproperty calledlocationthat allows it to be placed on the grid (like a Ship). - a constant
Stringproperty calledexplosionTextthat will appear if the mine is hit.
Note: The use of constant properties is related to our use of _Mine_ and protocols. More on this later!
Once you’ve defined the Mine struct, you’ll want to instantiate some mines and add them to the grid. So after the code for adding ships (in addShipsAndMines), add the code necessary to create two mines and add them to the grid. Instead of using addShipToGrid, as you did for adding ships to the grid, you will use addMineToGrid. Add the two mines so that a different message appears when each mine is hit.
When you are done, test your new code by hitting play, and complete a match against the computer.